package com.sgcc.pms.tocs.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import com.sgcc.pms.tocs.entity.SecurityUser;

@Component
public class CustomToken implements TokenEnhancer {

	@Override
	public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
		SecurityUser user = (SecurityUser)authentication.getPrincipal();
		final Map<String, Object> additionalInfo = new HashMap<>();
		additionalInfo.put("userId", user.getId());
		additionalInfo.put("name", user.getUsername());
		((DefaultOAuth2AccessToken)accessToken).setAdditionalInformation(additionalInfo);
		return accessToken;
	}

}
